package com.king.zxing.app;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.king.zxing.app.dao.GoodsDao;
import com.king.zxing.app.dao.UserDao;
import com.king.zxing.app.domain.Goods;

public class GoodsDetailActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_detail);

        long goods_id = getIntent().getLongExtra("goods_id", 1);
        long user_id = getIntent().getLongExtra("user_id", 1);

        Log.e("cloyir", "GoodsDetailActivity" + goods_id + "," + user_id);

        Goods goods = new GoodsDao(this).getGoodsByID(goods_id);
        if (goods == null) {
            finish();
            return;
        }
        ((ImageView) findViewById(R.id.imageView)).setImageResource(Goods.askImageResource((int) goods.imageID));
        ((TextView) findViewById(R.id.goods_detail_price)).setText(goods.price);
        ((TextView) findViewById(R.id.goods_detail_name)).setText(goods.name);

        View button_return = findViewById(R.id.button_return);
        button_return.setOnClickListener(v -> finish());

        Button button_buy = findViewById(R.id.button_buy);
        Log.e("cloyir", "BuyInit");
        button_buy.setOnClickListener(v -> {
            Log.e("cloyir", "Buy");
            new UserDao(this).SubPointsByUserID(user_id, Integer.parseInt(goods.price));
            finish();
        });
    }
}
